package com.cqupt.qualityevaluation.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cqupt.qualityevaluation.dao.*;
import com.cqupt.qualityevaluation.pojo.*;
import com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
// 返回树状结构源数据

@RestController
public class EvaluateTreeController {

    // 全局变量
    public static final String TIME_FULL_SPRIT = "yyyy-MM-dd HH:mm:ss";

    /**
     * 时间转换成字符串
     *
     * @param date 日期
     * @param formartStr 格式
     * @retur
     */
    public static String dateToString(Date date, String formartStr) {
        String strDate = null;
        if (date != null && formartStr != null && !"".equals(formartStr)) {
            SimpleDateFormat sdf = new SimpleDateFormat(formartStr);
            strDate = sdf.format(date);
        }
        return strDate;
    }

    public Integer getGoodId(String schoolname){
        QueryWrapper<evaluatetaskarrange> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("schoolname",schoolname);
        List<evaluatetaskarrange> evaluatetaskarranges = evaluateTaskArrangeMapper.selectList(queryWrapper);
        List taskIDs = new ArrayList(); //当前学校被安排的任务列表
        for (evaluatetaskarrange evaluatetaskarrange : evaluatetaskarranges) {
            taskIDs.add(evaluatetaskarrange.getEvaluatetaskid());
        }
//        System.out.println(taskIDs);

        QueryWrapper<evaluatetask> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.in("evaluatetaskid",taskIDs);
        List<evaluatetask> evaluatetasks = evaluateTaskMapper.selectList(queryWrapper1);
//        List taskEndtimes =new ArrayList<>();
        //  调用日期转字符串方法,时间戳格式也有严格要求
        Timestamp now = Timestamp.valueOf( dateToString(new Date(), TIME_FULL_SPRIT));
        Integer goodTaskID = -1;
        for (evaluatetask evaluatetask : evaluatetasks) {
            if (evaluatetask.getEvaluatetaskendtime().getTime() > now.getTime() && now.getTime()> evaluatetask.getEvaluatetaskopentime().getTime()){
                goodTaskID =evaluatetask.getEvaluatetaskid(); // 拿到当前合适的评估任务id
                break;
            }
        }
        return goodTaskID;
    }



    @Autowired
    private EvaluateTaskMapper evaluateTaskMapper;
    @Autowired
    private EvaluateTaskArrangeMapper evaluateTaskArrangeMapper;
    @Autowired
    private EvaluateIndex1Mapper evaluateIndex1Mapper;
    @Autowired
    private EvaluateIndex2Mapper evaluateIndex2Mapper;
    @Autowired
    private EvaluateIndex3Mapper evaluateIndex3Mapper;

    /**
     * 找到在进行中的被安排的任务
     * @param schoolname
     * @return
     */
    @RequestMapping("/evaluatetree/{schoolname}")
    public List getTree(@PathVariable("schoolname") String schoolname){
        Integer goodTaskID = getGoodId(schoolname);
        QueryWrapper<evaluatetask> queryWrapper2 = new QueryWrapper<>();
        queryWrapper2.eq("evaluatetaskid", goodTaskID);
        Integer evaluateid=evaluateTaskMapper.selectOne(queryWrapper2).getEvaluateid(); //拿到evaluateID
        QueryWrapper<evaluateindex1> queryWrapper3 = new QueryWrapper<>();
        queryWrapper3.in("evaluateid", evaluateid);
        List<evaluateindex1> evaluateindex1s = evaluateIndex1Mapper.selectList(queryWrapper3);

        List l1=new ArrayList<>();
        for (evaluateindex1 evaluateindex1 : evaluateindex1s) {
            l1.add(evaluateindex1.getEvaluateindex1id());
        }
        QueryWrapper<evaluateindex2> queryWrapper4 =new QueryWrapper<>();
        queryWrapper4.in("evaluateindex1id", l1);
        List<evaluateindex2> evaluateindex2s = evaluateIndex2Mapper.selectList(queryWrapper4);
        List l2=new ArrayList<>();
        for (evaluateindex2 evaluateindex2 : evaluateindex2s) {
            l2.add(evaluateindex2.getEvaluateindex2id());
        }
        QueryWrapper<evaluateindex3> queryWrapper5 =new QueryWrapper<>();
        queryWrapper5.in("evaluateindex2id",l2);
        List<evaluateindex3> evaluateindex3s = evaluateIndex3Mapper.selectList(queryWrapper5);


        List eva = evaluateindex1s;
         eva.addAll(evaluateindex2s);
         eva.addAll(evaluateindex3s);
        return eva;
        }

    @RequestMapping("/getRightTaskId/{schoolname}")
    public Integer getRightTaskId(@PathVariable("schoolname") String schoolname){
        return getGoodId(schoolname);
    }
}

